/**
 * 
 */
package com.ly.hotel.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ly.hotel.R;
import com.ly.hotel.model.AndroidAppInfo;


/**
 * @author jouson
 * 2014-6-11
 */
public class AppUtil {
	
	/**
	 * 获取所有已经安装的apk信息
	 * @param context
	 * @return
	 */
	public static List<AndroidAppInfo> getInstalledAppInfos(Context context) {
		PackageManager packageManager = context.getPackageManager();
		List<PackageInfo> installedPackages = packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES);
		List<AndroidAppInfo> androidAppInfos = new ArrayList<AndroidAppInfo>();
		for (PackageInfo p : installedPackages) {
			AndroidAppInfo aai = new AndroidAppInfo();
			aai.setAppName(p.applicationInfo.loadLabel(packageManager).toString());
			aai.setIcon(p.applicationInfo.loadIcon(packageManager));
			aai.setPackageName(p.packageName);
			aai.setFlags(p.applicationInfo.flags);
			if (aai.getPackageName() == null || aai.getPackageName().trim().equals("")) {
				continue;
			}
//			if((p.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0){
				androidAppInfos.add(aai);
//			}
		}
		return androidAppInfos;
	}
	
	public static void startApp(Context context, String appPackageName){
	    try{
	        Intent intent = context.getPackageManager().getLaunchIntentForPackage(appPackageName);
	        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	        context.startActivity(intent);
	    }catch(Exception e){
	    	e.printStackTrace();
	        Toast.makeText(context, "没有安装", Toast.LENGTH_LONG).show();
	    }
	}

	public static void startApp(Context context, String appPackageName,String mac,String hotelId,
								String hotelName,String roomId){
		try{
			Intent intent = context.getPackageManager().getLaunchIntentForPackage(appPackageName);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra("mac",mac);
			intent.putExtra("hotelId",hotelId);
			intent.putExtra("hotelName",hotelName);
			intent.putExtra("roomId",roomId);
			intent.putExtra("channel","premier");
			context.startActivity(intent);
		}catch(Exception e){
			e.printStackTrace();
			Toast.makeText(context, "没有安装", Toast.LENGTH_LONG).show();
		}
	}

	public static boolean checkAppIsExist(Context context, String packageName) {
		try {
			PackageInfo pin = context.getPackageManager().getPackageInfo(
					packageName, 0);
			if (pin != null) {
				return true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
		return false;
	}
	public static void showToastPosition(Context context,String str){
		LayoutInflater lin = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = lin.inflate(R.layout.remindtext, null);
		TextView text = (TextView) view.findViewById(R.id.text);
		text.setText(str);
		Toast toast = new Toast(context);
		toast.setView(view);
		toast.setDuration(Toast.LENGTH_LONG );
		toast.show();
	}
	
	public static int getVersion(Context context,String packageName){
		try {
			PackageInfo p = context.getPackageManager().getPackageInfo(packageName, 0);
			if(p != null){
				return p.versionCode;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return -1;
	}
	
	public static int getArchiveVersion(Context context,String apk_path){
		try {
			PackageManager pm = context.getPackageManager();
            PackageInfo packageInfo = pm.getPackageArchiveInfo(apk_path, PackageManager.GET_ACTIVITIES);
			if(packageInfo != null){
				return packageInfo.versionCode;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return -1;
	}
	
	public static String getVersionName(Context context,String packageName){
		try {
			PackageInfo p = context.getPackageManager().getPackageInfo(packageName, 0);
			if(p != null){
				return p.versionName;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	/*通过读取本地的apk文件获取该apk的包名，返回正确的包名(如com.togic.livevideo)，获取失败就返回null*/
	public static String getApkPacketName(Context context,String path){
		String name = null;

		File file = new File(path);
		if(file.exists()){
			PackageManager pm = context.getPackageManager();
	        PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
	        ApplicationInfo appInfo = null;
	        if (info != null) {
	            appInfo = info.applicationInfo;
	            String packageName = appInfo.packageName;
	            name = packageName;
	        }
		}
        
        return name;
	}
	
    public static boolean copyApkFromAssets(Context context, String fileName, String path) {  
        boolean copyIsFinish = false;  
        try {  
            InputStream is = context.getAssets().open(fileName);  
            File file = new File(path);  
            file.createNewFile();  
            FileOutputStream fos = new FileOutputStream(file);  
            byte[] temp = new byte[1024];  
            int i = 0;  
            while ((i = is.read(temp)) > 0) {  
                fos.write(temp, 0, i);  
            }  
            fos.close();  
            is.close();  
            copyIsFinish = true;  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return copyIsFinish;  
    }  

}
